﻿using System;
using VideoStore.Business.Facade;
using VideoStore.Common.Response;
using VideoStore.Domain.Entities;
using Warehouse.Services.Base;
using Warehouse.Services.Dto;
using Warehouse.Services.Factory;
using Warehouse.Services.Interface;
using Warehouse.Services.Translation;
using VideoStore.Common.Response.Extension;

namespace Warehouse.Services
{
    public class DeliveryService : IDeliveryService
    {
        #region Implementation of IDeliveryService

        /// <summary>
        /// Creates the delivery.
        /// </summary>
        /// <param name="newDeliveryDto">The new delivery dto.</param>
        public Response<DeliveryDto> CreateDelivery(DeliveryDto newDeliveryDto)
        {
            var response = new Response<DeliveryDto> { IsSuccessful = true };
            try
            {
                var facade = VideoStoreFacade.Instance;
                ITranslator translator = TranslatorFactory.GetTranslatorByType(typeof(DeliveryTranslator));
                var deliveryEntity = translator.TranslateToEntity<DeliveryDto, Delivery>(newDeliveryDto);
                Delivery savedEntity = facade.CreateDelivery(deliveryEntity);
                DeliveryDto deliveryDto = translator.TranslateToDto<Delivery, DeliveryDto>(savedEntity);
                response.ResponseObject = deliveryDto;
                response.Messages.AddSuccessfulSaveMessage("Delivery");
            }
            catch (Exception ex)
            {
                response.IsSuccessful = false;
                response.Errors.AddFatalError(ex.Message);
            }
            return response;
        }

        /// <summary>
        /// Gets the delivery by id.
        /// </summary>
        /// <param name="deliveryId">The delivery id.</param>
        /// <returns></returns>
        public Response<DeliveryDto> GetDeliveryById(Guid deliveryId)
        {
            var response = new Response<DeliveryDto> { IsSuccessful = true };
            try
            {
                var facade = VideoStoreFacade.Instance;
                ITranslator translator = TranslatorFactory.GetTranslatorByType(typeof(DeliveryTranslator));
                var deliveryEntity = facade.GetDeliveryById(deliveryId);
                DeliveryDto deliveryDto = translator.TranslateToDto<Delivery, DeliveryDto>(deliveryEntity);
                response.ResponseObject = deliveryDto;
            }
            catch (Exception ex)
            {
                response.IsSuccessful = false;
                response.Errors.AddFatalError(ex.Message);
            }
            return response;
        }

        #endregion
    }
}